package com.heima.interceptor;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.heima.common.context.BaseContext;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /*
     * 插入数据时填充字段
     * */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "createBy", String.class, BaseContext.getCurrentId() + "");
    }

    /*
     * 更新数据时填充字段
     * */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.setFieldValByName("updateBy", BaseContext.getCurrentId() + "", metaObject);
    }
}
